<template>
    <div class="particles-container">
        <div id="particles-js"></div>
    </div>

    <div class="form_wrap">
        <el-form ref="formRef" :model="registerData" label-width="100px" class="demo-dynamic">
            <el-form-item prop="username" label="用户名" :rules="[
                { required: true, message: '此项必填', trigger: 'blur' }
            ]">
                <el-input v-model="registerData.username" />
            </el-form-item>
            <el-form-item prop="password" label="密码" :rules="[
                { required: true, message: '此项必填', trigger: 'blur' }
            ]">
                <el-input type="password" v-model="registerData.password" />
            </el-form-item>
            <el-form-item prop="repassword" label="确认密码" :rules="[
                { required: true, message: '此项必填', trigger: 'blur' },
                { validator: validatePassword, trigger: 'blur' }
            ]">
                <el-input type="password" v-model="registerData.repassword" />
            </el-form-item>
        </el-form>
        <div class="flex">
            <el-button type="primary" class="login_btn" @click="handleBack">返回登录</el-button>
            <el-button type="primary" class="login_btn" @click="handleRegister">注册</el-button>
        </div>
    </div>
</template>

<script>
import { reactive, toRefs, onMounted, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { registerApi } from "@/util/request"
import 'particles.js';

export default {
    name: 'Register',
    setup() {
        const store = useStore()
        const router = useRouter()
        const formRef = ref(null) // 引用表单
        const data = reactive({
            registerData: {
                username: "",
                password: "",
                repassword: ""
            },
        })

        const validatePassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('确认密码不能为空'))
            } else if (value !== data.registerData.password) {
                callback(new Error('两次输入的密码不一致'))
            } else {
                callback()
            }
        }

        const handleRegister = () => {
            formRef.value.validate((valid) => { // 使用引用访问表单
                if (valid) {
                    console.log('注册数据:', data.registerData)
                    // 请求后台接口
                    registerApi(data.registerData).then(res => {
                        console.log(res)
                        if(res.status === 0 ){
                            router.push({
                                path:'/login'
                            })
                        }
                    }).catch(err => {
                        console.error(err)
                        // 处理注册错误
                    })
                } else {
                    console.log('表单验证失败')
                    return false
                }
            })
        }

        const handleBack = () => {
            router.push({ path: '/login' })
        }

        onMounted(() => {
            particlesJS("particles-js", {
                particles: {
                    number: { value: 133, density: { enable: true, value_area: 800 } },
                    color: { value: "#ffffff" },
                    shape: {
                        type: "circle",
                        stroke: { width: 0, color: "#000000" },
                        polygon: { nb_sides: 5 },
                        image: { src: "img/github.svg", width: 100, height: 100 }
                    },
                    opacity: {
                        value: 0.5,
                        random: false,
                        anim: { enable: false, speed: 1, opacity_min: 0.1, sync: false }
                    },
                    size: {
                        value: 3,
                        random: true,
                        anim: { enable: false, speed: 40, size_min: 0.1, sync: false }
                    },
                    line_linked: {
                        enable: true,
                        distance: 150,
                        color: "#ffffff",
                        opacity: 0.4,
                        width: 1
                    },
                    move: {
                        enable: true,
                        speed: 6,
                        direction: "none",
                        random: false,
                        straight: false,
                        out_mode: "out",
                        bounce: false,
                        attract: { enable: false, rotateX: 600, rotateY: 1200 }
                    }
                },
                interactivity: {
                    detect_on: "canvas",
                    events: {
                        onhover: { enable: true, mode: "repulse" },
                        onclick: { enable: true, mode: "push" },
                        resize: true
                    },
                    modes: {
                        grab: { distance: 400, line_linked: { opacity: 1 } },
                        bubble: { distance: 400, size: 40, duration: 2, opacity: 8, speed: 3 },
                        repulse: { distance: 200, duration: 0.4 },
                        push: { particles_nb: 4 },
                        remove: { particles_nb: 2 }
                    }
                },
                retina_detect: true
            });
        });

        return {
            ...toRefs(data),
            formRef,
            handleRegister,
            validatePassword,
            handleBack,
        }
    }
}
</script>

<style scoped>
.login_wrap {
    width: 100%;
    height: 100vh;
    background-color: rgb(76, 35, 88);
    position: relative;
}

.form_wrap {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    padding: 30px 50px;
    border-radius: 5px;
}

.login_btn {
    display: block;
    margin: 10px auto;
}
</style>

<!-- 背景 -->
<style scoped>
body {
    margin: 0;
    font: normal 75% Arial, Helvetica, sans-serif;
}

canvas {
    display: block;
    vertical-align: bottom;
}

#particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: #311a59;
    background-image: url("");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50% 50%;
}

.count-particles {
    background: #000022;
    position: absolute;
    top: 48px;
    left: 0;
    width: 80px;
    color: #13E8E9;
    font-size: .8em;
    text-align: left;
    text-indent: 4px;
    line-height: 14px;
    padding-bottom: 2px;
    font-family: Helvetica, Arial, sans-serif;
    font-weight: bold;
}

.js-count-particles {
    font-size: 1.1em;
}

#stats,
.count-particles {
    -webkit-user-select: none;
    margin-top: 5px;
    margin-left: 5px;
}

#stats {
    border-radius: 3px 3px 0 0;
    overflow: hidden;
}

.count-particles {
    border-radius: 0 0 3px 3px;
}
</style>
